package com.wuseguang.springcloud.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wuseguang.springcloud.JdbcTemplate.WXJdbc;
import com.wuseguang.springcloud.dao.jdbc.TextEvaluationJdbc;
import com.wuseguang.springcloud.dao.repository.TextEvaluationRepositoty;
import com.wuseguang.springcloud.entity.TextEvaluation;
import com.wuseguang.springcloud.service.TextEvaluationService;
import com.wuseguang.springcloud.util.DateUtil;
import com.wuseguang.springcloud.util.Result;
import com.wuseguang.springcloud.util.WeChat.TemplateTools;
import com.wuseguang.springcloud.util.WeChat.WXUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文字评价表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-02-12
 */
@Service
public class TextEvaluationServiceImpl implements TextEvaluationService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TextEvaluationRepositoty textEvaluationRepositoty;
    @Autowired
    private TextEvaluationJdbc textEvaluationJdbc;
    @Autowired
    private WXJdbc wXJdbc;

    @Override
    public Result addTextEvaluation(TextEvaluation textEvaluation) {

        logger.info("给学生添加文字点评 参数如下 = {}", textEvaluation.toString());
        textEvaluation.setAddTime(new Date());
        textEvaluationRepositoty.save(textEvaluation);
        try {
            officialAccountPush(textEvaluation);
        }catch (Exception e){
            logger.error("e",e);
        }
        return new Result();
    }

    /**
     * 公众号推送
     *
     * @param textEvaluation
     */
    public void officialAccountPush(TextEvaluation textEvaluation) {

        //获取token
        String accessToken = wXJdbc.getOfficialAccountAccessToken(0);
        //查询家长的小程序openId
        String sql = "select p.open_id_one,p.id,p.open_id_two  from parent p  LEFT JOIN parent_student ps on(ps.parent_id=p.id)where ps.student_id=?";


        //家长openid
        Map<String, Object> parent = wXJdbc.queryMap(sql, textEvaluation.getStudentId());
        String openId = (String) parent.get("open_id_two");

        //判断openId是否为空
        if (!StringUtils.isEmpty(openId)) {

            //查询学生名字
            sql = "select student_name string from student where id = ?";
            String studenName = wXJdbc.queryString(sql, textEvaluation.getStudentId());

            Map<String, Object> pushMap = new HashMap<>();
            pushMap.put("first", "家长您好，您的孩子有一条新的评价信息。");
            pushMap.put("keyword1", studenName);
            pushMap.put("keyword2", textEvaluation.getTeacherName());
            pushMap.put("keyword3", textEvaluation.getContent());
            pushMap.put("keyword4", "");
            pushMap.put("keyword5", DateUtil.time());
            pushMap.put("remark", "感谢您的支持。");

            //跳转小程序信息
            JSONObject appletJson= new JSONObject();
            appletJson.put("appid","wx30b606bac7775eba");
            appletJson.put("pagepath","pages/evaluate/index");

            //解析后的数据
            JSONObject json = TemplateTools.initializationData(pushMap);
            //推送公众号信息
            WXUtil.officialAccountPush(accessToken, openId, "aPysfnjC1jCISCqp8RstlJwxZkKd93jXkrollyWrrlg", "", appletJson, json);
        }

    }
}
